package com.example.app.controller;

import com.example.app.annotations.Auth;
import com.example.app.common.BaseResponse;
import com.example.app.common.ErrorCode;
import com.example.app.common.Role;
import com.example.app.entity.dto.DeleteRequest;
import com.example.app.entity.dto.LoginDTO;
import com.example.app.entity.dto.RegisterDTO;
import com.example.app.entity.dto.ResetPasswordDTO;
import com.example.app.entity.dto.UpdateUserDTO;
import com.example.app.entity.dto.UserManageDTO;
import com.example.app.entity.dto.UserQueryRequest;
import com.example.app.entity.dto.ChangePasswordDTO;
import com.example.app.entity.vo.UserPageVO;
import com.example.app.entity.vo.UserVO;
import com.example.app.service.UserService;
import com.example.app.utils.ResultUtils;
import com.example.app.utils.ThrowUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param registerDTO 注册包装类
     * @return 是否注册成功
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody RegisterDTO registerDTO) {
        ThrowUtils.throwIf(registerDTO == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.register(registerDTO));
    }

    /**
     * 用户登录
     *
     * @param loginDTO 登录封装类
     * @param request 请求信息
     * @return 用户信息
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> login(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(loginDTO == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.login(loginDTO, request));
    }

    /**
     * 获取登录用户信息
     *
     * @param request 请求信息
     * @return 用户信息
     */
    @PostMapping("/getLoginUserInfo")
    public BaseResponse<UserVO> getLoginUserInfo(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求信息为空");
        return ResultUtils.success(userService.getLoginUserInfo(request));
    }

    /**
     * 登出
     *
     * @param request 请求信息
     */
    @PostMapping("/loginOut")
    public BaseResponse<Void> loginOut(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求信息为空");
        userService.loginOut(deleteRequest, request);
        return ResultUtils.success(null);
    }

    /**
     * 封禁用户
     *
     * @param request 请求信息
     */
    @PostMapping("/banUser")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<Void> banUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求信息为空");
        userService.banUser(deleteRequest, request);
        return ResultUtils.success(null);
    }

    /**
     * 解封用户
     *
     * @param request 请求信息
     */
    @PostMapping("/unBanUser")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<Void> unBanUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求信息为空");
        userService.unBanUser(deleteRequest, request);
        return ResultUtils.success(null);
    }

    /**
     * 更新用户信息
     *
     * @param updateUserDTO 用户信息更新请求
     * @param request 请求信息
     * @return 更新后的用户信息
     */
    @PostMapping("/updateUserInfo")
    public BaseResponse<UserVO> updateUserInfo(@RequestBody UpdateUserDTO updateUserDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(updateUserDTO == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.updateUserInfo(updateUserDTO, request));
    }
    
    /**
     * 分页获取用户列表（管理员接口）
     *
     * @param userQueryRequest 查询条件
     * @param request 请求信息
     * @return 用户分页列表
     */
    @PostMapping("/list/page")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<UserPageVO> listUsersByPage(@RequestBody UserQueryRequest userQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.listUsersByPage(userQueryRequest));
    }
    
    /**
     * 添加用户（管理员接口）
     *
     * @param userManageDTO 用户信息
     * @param request 请求信息
     * @return 添加的用户信息
     */
    @PostMapping("/add")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<UserVO> addUser(@RequestBody UserManageDTO userManageDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(userManageDTO == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.addUser(userManageDTO, request));
    }
    
    /**
     * 更新用户（管理员接口）
     *
     * @param userManageDTO 用户信息
     * @param request 请求信息
     * @return 更新后的用户信息
     */
    @PostMapping("/update")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<UserVO> updateUser(@RequestBody UserManageDTO userManageDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(userManageDTO == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(userManageDTO.getId() == null, ErrorCode.PARAMS_ERROR, "用户ID不能为空");
        return ResultUtils.success(userService.updateUser(userManageDTO, request));
    }
    
    /**
     * 删除用户（管理员接口）
     *
     * @param deleteRequest 删除请求
     * @param request 请求信息
     * @return 是否成功
     */
    @PostMapping("/delete")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.deleteUser(deleteRequest, request));
    }
    
    /**
     * 重置用户密码（管理员接口）
     *
     * @param resetPasswordDTO 重置密码请求
     * @param request 请求信息
     * @return 是否成功
     */
    @PostMapping("/resetPassword")
    @Auth(mustRole = Role.Admin)
    public BaseResponse<Boolean> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(resetPasswordDTO == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(resetPasswordDTO.getUserId() == null, ErrorCode.PARAMS_ERROR, "用户ID不能为空");
        ThrowUtils.throwIf(resetPasswordDTO.getNewPassword() == null, ErrorCode.PARAMS_ERROR, "新密码不能为空");
        return ResultUtils.success(userService.resetPassword(resetPasswordDTO, request));
    }

    /**
     * 修改密码
     *
     * @param changePasswordDTO 密码修改请求
     * @param request 请求信息
     * @return 是否成功
     */
    @PostMapping("/changePassword")
    public BaseResponse<Boolean> changePassword(@RequestBody ChangePasswordDTO changePasswordDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(changePasswordDTO == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(StrUtil.hasBlank(changePasswordDTO.getOldPassword(), changePasswordDTO.getNewPassword()), 
                           ErrorCode.PARAMS_ERROR, "密码不能为空");
        return ResultUtils.success(userService.changePassword(changePasswordDTO, request));
    }
}
